package net.sxnic.workflow.whimpl;

import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.handler.AbstractHandler;
import net.sxnic.workflow.handler.HandlerExt;
import net.sxnic.workflow.handler.WfHandlerException;
import net.sxnic.workflow.wfentity.WfEntity;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 保存实体处理类
 * 
 * @author 孙宇飞
 * 
 */
public class SaveCommentHandler extends AbstractHandler<WfEntity> implements
		HandlerExt<WfEntity> {

	private static Logger logger = LoggerFactory
			.getLogger(SaveCommentHandler.class);

	public SaveCommentHandler(WfContextImpl<WfEntity> context) {
		super(context);
	}

	@Override
	public void editExecute() throws WfHandlerException {

	}

	@Override
	public void saveExecute() throws WfHandlerException {
		// 保存意见
		if (StringUtils.isNotBlank(context.getTaskComment())) {
			context.getTaskManager().updateComment(context.getTask().getId(),
					context.getTaskComment());
			logger.debug("===保存意见成功===" + context.getTaskComment());
		} else {
			logger.debug("===保存意见未成功===意见为空====");
		}

	}

	@Override
	public void viewExecute() throws WfHandlerException {

	}

	@Override
	public void submitExecute() throws WfHandlerException {
		//如果必须有意见，则需要抛出异常
		if(StringUtils.contains(context.getTask().getNode().getValidate(), "comment:required") && StringUtils.isBlank(context.getTaskComment())){
			context.setErrorMsg("请填写意见之后再签字发送！");
			throw new WfHandlerException("===节点"+context.getTaskName()+"==在签字发送时，意见为空===");
		}
		// 保存意见
		if (StringUtils.isNotBlank(context.getTaskComment())) {
			context.getTaskManager().updateComment(context.getTask().getId(),
					context.getTaskComment());
			logger.debug("===保存意见成功===" + context.getTaskComment());
		} else {
			logger.debug("===保存意见未成功===意见为空====");
		}
	}

}
